#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 2) {
        cout << "Usage: " << argv[0] << " file" << endl;
        return 0;
    }

    fstream fs;
    fs.open(argv[1]);
    if (!fs.is_open()) {
        cout << "File " << argv[1] << " open error!" << endl;
        return 1;
    }

    vector<string> input;
    string str;
    while (!fs.eof()) {
        str.clear();
        fs >> str;
        cout << str << endl;

        if (str.empty()) {
            continue;
        }

        input.push_back(str);
    }
    fs.close();

    vector<string> output;
    output = traverseGraph(input);

    vector<string>::iterator it;
    cout << "Input main function" << endl;
    for (it = input.begin(); it != input.end(); ++it) {
        cout << *it << endl;
    }

    cout << "c++ program Output" << endl;
    for (it = output.begin(); it != output.end(); ++it) {
        cout << *it << endl;
    }

    cout << endl;

    // create a string array
    char **input_array = (char **)malloc(sizeof(char *) * input.size());
    if (input_array == NULL) {
        return 1;
    }

    size_t i;
    for (i = 0; i < input.size(); i++) {
        input_array[i] = strdup(input[i].c_str());
    }

    char **output_array;
    size_t output_size;
    output_array = graphTraverseC(input_array, input.size(), &output_size);

    cout << "C program output:" << endl;
    for (i = 0; i < output_size; i++) {
        cout << output_array[i] << endl;
    }

    cout << endl;

    for (i = 0; i < input.size(); i++) {
        free(input_array[i]);
    }
    free(input_array);
    for (i = 0; i < output_size; i++) {
        free(output_array[i]);
    }
    free(output_array);

    return 0;
}

